package com.nt.train.controller;
import com.alibaba.fastjson.JSON;
import com.nt.train.dto.AuthInfo;
import com.nt.train.service.AuthLink;
import com.nt.train.service.AuthService;
import com.nt.train.service.impl.Level1AuthLink;
import com.nt.train.service.impl.Level2AuthLink;
import com.nt.train.service.impl.Level3AuthLink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * @author hlf
 * @version 1.0
 * @date 2022/8/8 10:36
 */
@RestController
public class AuthController {

    private Logger logger = LoggerFactory.getLogger(AuthController.class);


    private SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 时间格式化

    public AuthInfo doAuth(String uId, String orderId, Date authDate) throws ParseException {

        // 三级审批
        Date date = AuthService.queryAuthInfo("1000013", orderId);
        if (null == date){
            return new AuthInfo("0001", "单号：", orderId, " 状态：待三级审批负责人 ", "王工");
        }

        // 二级审批
        if (authDate.after(f.parse("2020-06-01 00:00:00")) && authDate.before(f.parse("2020-06-25 23:59:59"))) {
            date = AuthService.queryAuthInfo("1000012", orderId);
            if (null == date) {
                return new AuthInfo("0001", "单号：", orderId, " 状态：待二级审批负责人 ", "张经理");
            }
        }

        // 一级审批
        if (authDate.after(f.parse("2020-06-11 00:00:00")) && authDate.before(f.parse("2020-06-20 23:59:59"))) {
            date = AuthService.queryAuthInfo("1000011", orderId);
            if (null == date){
                return new AuthInfo("0001", "单号：", orderId, " 状态：待一级审批负责人 ", "段总");
            }
        }

        return new AuthInfo("0001", "单号：", orderId, " 状态：审批完成");
    }

    public AuthInfo test_AuthLink(String uId, String orderId, Date authDate) throws ParseException {

        AuthLink authLink = new Level3AuthLink("1000013", "王工")
                .appendNext(new Level2AuthLink("1000012", "张经理")
                        .appendNext(new Level1AuthLink("1000011", "段总")));

        logger.info("测试结果：{}", JSON.toJSONString(authLink.doAuth("小傅哥", "1000998004813441", new Date())));

        // 模拟三级负责人审批
        AuthService.auth("1000013", "1000998004813441");
        logger.info("测试结果：{}", "模拟三级负责人审批，王工");
        logger.info("测试结果：{}", JSON.toJSONString(authLink.doAuth("小傅哥", "1000998004813441", new Date())));

        // 模拟二级负责人审批
        AuthService.auth("1000012", "1000998004813441");
        logger.info("测试结果：{}", "模拟二级负责人审批，张经理");
        logger.info("测试结果：{}", JSON.toJSONString(authLink.doAuth("小傅哥", "1000998004813441", new Date())));

        // 模拟一级负责人审批
        AuthService.auth("1000011", "1000998004813441");
        logger.info("测试结果：{}", "模拟一级负责人审批，段总");
        logger.info("测试结果：{}", JSON.toJSONString(authLink.doAuth("小傅哥", "1000998004813441", new Date())));

        return new AuthInfo("0001", "单号：", orderId, " 状态：待三级审批负责人 ", "王工");
    }

}